<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use app\common\model\TaocanOrder;

/**
 * 套餐模块
 *
 * @author Administrator
 */
class Taocan extends Api {
    
    //put your code here
    public function create() {
        $user_info = model('user')->get($this->userId);
        if(!$user_info->isbuy) {
            return [
                'status' => false,
                'data' => '',
                'msg' => '用户被限制购买, 请联系客服',
            ];
        }
        $request = new Request();
        $taocanModel = new TaocanOrder();
        $data = $request->post();
        if($data['taocan'] == ""){
            return [
                'status' => false,
                'data' => '',
                'msg' => '请选择套餐',
            ];
        }
        $data['taocan'] = json_decode($data['taocan'], true);
        $create_date = [
            'user_id' => $this->userId,
            'room_bn' => $data['room_bn'],
            'taocan' => json_encode($data['taocan'], JSON_UNESCAPED_SLASHES),
            'taocan_name' => $data['taocan']['name'],
            'price' => $data['taocan']['price']
        ];
        //后结
        if($data['payment_code'] == 'after_pay') {
            $create_date['payment_code'] = 'after_pay';
            $create_date['pay_status'] = '1';
            $create_date['after_status'] = 0;
        }
        $validate = new \app\common\validate\Taocan();
        $result = $validate->check($create_date);
        $hour=date("G");
        if($hour < 3) {
            $create_date['ctime'] = strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:58");
        }
        if (!$result) {
            return [
                'status' => false,
                'data' => '',
                'msg' => $validate->getError()
            ];
        }
        if ($taocanModel->save($create_date)) {
            return [
                'status' => true,
                'data' => $taocanModel->id,
                'msg' => '套餐创建成功',
            ];
        } else {
            return [
                'status' => false,
                'data' => '',
                'msg' => '套餐创建失败',
            ];
        }
    }

    public function detail() {
        $request = new Request();
        $data = $request->post();
        $taocanModel = new TaocanOrder();
        $taocan_info = $taocanModel->get($data['id']);
        return [
            'status' => true,
            'data' => $taocan_info,
            'msg' => '获取成功',
        ];
    }

    public function taocanList() {
        $request = new Request();
        $page = $request->post('page', 1);
        $size = $request->post('size', 20);
        $taocanModel = new TaocanOrder();
        $limit = ($page - 1) * $size . ',' . $size;
        $begintime=strtotime(date("Y-m-d",strtotime("-3 day"))." 0:0:0");
        $taocan_list = $taocanModel->where('user_id', $this->userId)
                        ->where('is_del', 0)->where('pay_status', 1)
                        ->where('ctime', ['EGT',$begintime],['ELT',time()])
                        ->order('id desc')->limit($limit)->select();
        foreach ($taocan_list as $k=>$v) {
            $v->date = date("Y-m-d H:i",$v->ctime);
        }
        return [
            'status' => true,
            'data' => $taocan_list,
            'msg' => '获取成功',
        ];
    }

    public function cancel() {
        /*return [
            'status' => false,
            'data' => '',
            'msg' => '请联系工作人员取消',
        ];*/
        $request = new Request();
        $data = $request->post();
        if (!is_numeric($data['id'])) {
            return [
                'status' => true,
                'data' => '',
                'msg' => 'id不能为空',
            ];
        }

        $taocanModel = new TaocanOrder();
        $taocan = $taocanModel->get($data['id']);
        if ($taocan->status == '1' || $taocan->pay_status == '2') {
            return [
                'status' => true,
                'data' => '',
                'msg' => '订单不能取消',
            ];
        }
        if ($taocan->pay_status == '1') {
            if($taocan->payment_code != 'offline' && $taocan->payment_code != 'after_pay' ){
                $billPaymentsModel = new \app\common\model\BillPayments();
                $info = $billPaymentsModel->getSuccessPaymentInfo($taocan->id, 5);

                $paymentsModel = new \app\common\model\Payments();
                $paymentInfo = $paymentsModel->getPayment($taocan->payment_code, $paymentsModel::PAYMENT_STATUS_YES);
                $conf = json_decode($paymentInfo['params'], true);

                if ($taocan->payment_code == 'balance') {
                    $balanceModel = new \app\common\model\Balance();
                    $balanceModel->change($taocan->user_id, 2, $taocan->price, $taocan->id);
                } else {
                    $payment = \org\Payment::create($taocan->payment_code, $conf);       //'wechatpay' or alipa
                    $res = $payment->refund($info['trade_no'], 1);
                }
            }
        }
        $taocanModel->save(['pay_status' => 2],['id' => $data['id']]);
        return [
            'status' => true,
            'data' => '',
            'msg' => '取消成功',
        ];
    }

    public function del() {
        $request = new Request();
        $data = $request->post();
        if (!is_numeric($data['id'])) {
            return [
                'status' => true,
                'data' => '',
                'msg' => 'id不能为空',
            ];
        }
        $taocanModel = new TaocanOrder();
        $taocanModel->where('id', $data['id'])->update(['is_del' => 1]);
        return [
            'status' => true,
            'data' => '',
            'msg' => '删除成功',
        ];
    }
    
}
